﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Playables;

/// <summary>
/// Demo
/// Create:2018/9
/// Last Edit Data:2019/1/5
/// </summary>
public class Demo : MonoBehaviour {

	public PlayableDirector playableDirector;
    private string videoAdid = "9g308s9uunm5gkjc5l";
    public Player player;
    #region Unity
    void Start () {
        if (Helper.isInstructionMode) { return; }
		StartCoroutine (GameManager.Instance.ReadyStartBattle (playableDirector));
	}
	
	void Update () {
		if (Input.GetKeyDown (KeyCode.Space)) {
			GameManager.Instance.NextStage ();
			Debug.Log (GameManager.Instance.battleSatge);
		}
    }
    #endregion
    /// <summary>
    /// 结束对战并返回菜单
    /// </summary>
    public void EndBattle()
    {
        UIManager.Instance.uiDictionary.Clear();
        GameManager.Instance.battleSatge = GameManager.BattleStage.None;
        SceneManager.Instance.BackMenu();
    }

    /// <summary>
    /// 玩家结束该回合
    /// </summary>
    public void PlayerOverTurn()
    {
        if (UIManager.Instance.playerHandCardUI.Count <= GameManager.Instance.maxHandCardCount)
        {
            GameManager.Instance.canDorp = false;
            EventManager.Instance.UpdateBattleInfo("玩家回合结束");
            GameManager.Instance.NextStage();
        }
        else
        {
            GameManager.Instance.canDorp = true;
            UIManager.Instance.uiDictionary[UIManager.UiArea.DorpButton].SetActive(true);
            UIManager.Instance.uiDictionary[UIManager.UiArea.OverPlayerTurnButton].SetActive(false);
        }
    }

    /// <summary>
    /// 播放声音
    /// </summary>
    /// <param name="clip"></param>
    public void SetVoice(AudioClip clip)
    {
        AudioManager.Instance.PlayVoice(clip);
    }

    /// <summary>
    /// 确认丢弃卡牌
    /// </summary>
    public void ConfrimDorp()
    {
        EventManager.Instance.DorpCard();
        if (UIManager.Instance.playerHandCardUI.Count <= GameManager.Instance.maxHandCardCount)
        {
            UIManager.Instance.uiDictionary[UIManager.UiArea.OverPlayerTurnButton].SetActive(true);
            UIManager.Instance.uiDictionary[UIManager.UiArea.DorpButton].SetActive(false);
        }
    }

    public void TouchWatchAds()
    {
        TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);
        player.BeRecover(10);
        //创建激励视频关闭回调
        r.OnClose += (bol, val) =>
        {
            Debug.Log("关闭广告 ，是否已经看完" + bol);
            //看完广告获得提示
            if (bol)
            {
                player.BeRecover(10);
                MaidianNew.MaidianBackCallback(" ");
                Debug.Log("广告看完");
            }
            else
            {
                Debug.Log("广告未看完");
            }
        };

        //创建报错回调  
        r.OnError += (code, errorMessage) =>
        {

            Debug.Log("code:" + code + "错误信息：" + errorMessage);
        };

        //展示广告（新版本必须调用，不然无法展示出来广告）
        r.Show();

    }
}
